package com.peter.pointfandrectanglefdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyView extends View {

    private String TAG = "MyView ";

    private int mRadius;
    private int mX;
    private int mY;
    private float mCenterX;
    private float mCenterY;

    private RectF mRectF;

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.i(TAG, "MyView ");
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        Log.i(TAG, "draw");

        //RectF设置
        mRectF = new RectF();
        mRectF.left = mCenterX - mRadius;
        mRectF.top = mCenterY - mRadius;
        mRectF.right = mCenterX + mRadius;
        mRectF.bottom = mCenterY + mRadius;

        //        mArcPaint.setShadowLayer(500,100,100,Color.BLACK);
        int[] colors = new int[]{Color.RED, Color.GREEN, Color.BLUE};

        //矩形画笔
        Paint mRectPaint = new Paint();
        mRectPaint.setColor(Color.RED);
        mRectPaint.setAntiAlias(true);

        // through the style we can control the fill thing *****
//        mRectPaint.setStyle(Paint.Style.STROKE);

//        mRectPaint.setShader(new RadialGradient(100, 100, 80, colors, null,
//                Shader.TileMode.REPEAT));
        mRectPaint.setColor(Color.BLUE);
        mRectPaint.setStrokeWidth(20);
        mRectPaint.setStyle(Paint.Style.STROKE);

        mRectPaint.setShader(new RadialGradient(100, 100, 80, colors, null,
                Shader.TileMode.REPEAT));
        //画矩形
        canvas.drawRect(mRectF, mRectPaint);

        //圆弧画笔
        Paint mArcPaint = new Paint();
        mArcPaint.setAntiAlias(true);
        mArcPaint.setColor(Color.RED);


        mArcPaint.setColor(Color.RED);
        mArcPaint.setStrokeWidth(20);


        mArcPaint.setShader(new RadialGradient(100, 100, 80, colors, null,
                Shader.TileMode.REPEAT));


        //画圆弧
        //startAngle 圆弧开始位置，从3点钟方向开始
        //sweepAngle 圆弧弧度
        //useCenter ,false为空心圆弧，true为实心（扇形）圆弧
        canvas.drawArc(mRectF, 0, 180, true, mArcPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i(TAG, "onSizeChanged ");
        mX = w;
        mY = h;
        mRadius = w / 4;
        mCenterX = mX / 2;
        mCenterY = mY / 2;
    }
}

